package com.example.day02.fragment;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;

import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day02.R;
import com.example.day02.adapter.RlvAdapter;
import com.example.day02.bean.BelleBean;
import com.example.day02.contract.HomeContract;
import com.example.day02.presenter.HomePresenter;

public class HomeFragment extends Fragment implements HomeContract.View {

    private RecyclerView mRlv;
    private Context context;
    private RlvAdapter rlvAdapter;
    private static final String TAG = "HomeFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        View inflate = inflater.inflate(R.layout.home_fragment, container, false);
        initView(inflate);
        initPresenter();
        return inflate;
    }

    private void initPresenter() {
        HomePresenter homePresenter = new HomePresenter(this);
        homePresenter.getData();
    }

    private void initView(@NonNull final View itemView) {
        mRlv = (RecyclerView) itemView.findViewById(R.id.rlv);

        mRlv.setLayoutManager(new LinearLayoutManager(context));
        mRlv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));

        rlvAdapter = new RlvAdapter(context);
        mRlv.setAdapter(rlvAdapter);

        rlvAdapter.setOnItemClickListener(new RlvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                BelleBean.ResultsBean resultsBean = rlvAdapter.datas.get(position);
                initPopWindow(resultsBean);
            }
        });
    }

    private void initPopWindow(final BelleBean.ResultsBean resultsBean) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.pop_item, null);
        final PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setFocusable(true);//焦点
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.setOutsideTouchable(true);

        popupWindow.showAtLocation(mRlv, Gravity.CENTER, 0, 0);

        TextView tv_pop = inflate.findViewById(R.id.tv_pop);
        tv_pop.setText(resultsBean.getDesc());

        Button cancel = inflate.findViewById(R.id.cancel);//取消按钮
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        Button affirm = inflate.findViewById(R.id.affirm);//确认按钮
        affirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initNatification(resultsBean);
                popupWindow.dismiss();
            }
        });
    }

//    通知
    private void initNatification(BelleBean.ResultsBean resultsBean) {
        //通知管理器
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        //Android8.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //通知渠道管理对象
            NotificationChannel channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);//指示灯
            channel.setLightColor(Color.RED);//指示灯颜色
            channel.setShowBadge(true);//角标

            manager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(getActivity(), MyFragment.class);
        PendingIntent activity = PendingIntent.getActivity(getActivity(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);//

        Notification notification = new NotificationCompat.Builder(context, "id")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(resultsBean.get_id())
                .setContentText(resultsBean.getDesc())
                .setContentIntent(activity)
                .setAutoCancel(true)//自动消失
                .build();
        manager.notify(100, notification);
    }

    @Override
    public void onSuccess(BelleBean belleBean) {
        rlvAdapter.setDatas(belleBean.getResults());
    }

    @Override
    public void onFail(String errorMsg) {
        Log.d(TAG, "onFail: " + errorMsg);
    }
}
